const {
  createUser,
  getUserInfo,
  updateById,
  updateUserInfo,
} = require("../service/user.service");
const { userRegisterError } = require("../constant/err.type");
const {
  updateTopicUser,
  updateCommentInfo,
} = require("../service/topic.service");
const jwt = require("jsonwebtoken");
const { JWT_SECRET } = require("../config/config.default");

class UserController {
  async register(ctx, next) {
    try {
      //! 1. 获取数据
      //* console.log(ctx.request.body);
      const { user_name, password } = ctx.request.body;

      //! 2. 操作数据库
      const res = await createUser(user_name, password);
      //* console.log(res);

      //! 3. 返回结果
      //* ctx.body = ctx.request.body;
      ctx.body = {
        code: 0,
        message: "用户注册成功",
        result: {
          id: res.id,
          user_name: res.user_name,
          user_avatar: res.user_avatar,
          user_signature: res.user_signature,
        },
      };
    } catch (err) {
      console.error(err);
      ctx.app.emit("error", userRegisterError, ctx);
    }
  }

  async login(ctx, next) {
    const { user_name } = ctx.request.body;

    //*1. 获取用户信息(在token的payload中, 记录id, user_name)
    try {
      //! 从返回结果对象中剔除 password 属性, 将剩下的属性放到res对象中
      const { password, ...res } = await getUserInfo({ user_name });

      //? ctx.body = `欢迎回来, ${user_name}`;

      ctx.body = {
        code: 0,
        message: "用户登录成功",
        result: {
          //*  token: jwt.sign(res, JWT_SECRET),
          token: jwt.sign(res, "studyhard_2022", { expiresIn: "1d" }),
          user_info: res,
        },
      };
    } catch (err) {
      console.error("用户登录失败", error);
    }
  }

  async changePassword(ctx, next) {
    //* 1. 获取数据
    const id = ctx.state.user.id;
    const password = ctx.request.body.password;
    //!console.log(id);
    //!console.log(password);
    //* 2. 操作数据库
    if (await updateById({ id, password })) {
      const { password, ...res } = await getUserInfo({ id });
      ctx.body = {
        code: 0,
        message: "修改密码成功",
        result: "",
      };
    } else {
      ctx.body = {
        code: "10007",
        message: "修改密码失败",
        result: "",
      };
    }
    //* 3. 返回结果
  }

  async editUserInfo(ctx, next) {
    const id = ctx.state.user.id;
    console.log({
      id: id,
      user_info: ctx.request.body,
    });

    await updateTopicUser(
      id,
      ctx.request.body.user_name,
      ctx.request.body.user_avatar
    );

    await updateCommentInfo(
      id,
      ctx.request.body.user_name,
      ctx.request.body.user_avatar
    );

    const res = await updateUserInfo(id, ctx.request.body);
    if (res) {
      const { password, ...newInfo } = await getUserInfo({ id });
      ctx.body = {
        code: 0,
        message: "修改个人信息成功",
        result: newInfo,
      };
    } else {
      ctx.body = {
        code: "10008",
        message: "修改个人失败",
        result: "",
      };
    }
  }
}

module.exports = new UserController();
